package chapter1.fundamentals.example;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdRandom;

import java.awt.*;
import java.util.Arrays;

/**
 * @author quanlinlin on 2018/10/24 22:15.
 * @version 1.0
 */
public class DrawPicture {

    private static Color  useColor  = Color.BLUE;
    private static double penRadius = .005;

    static {
        StdDraw.setPenColor(useColor);
        StdDraw.setPenRadius(penRadius);
    }

    public static void main(String[] args) {
        //drawRandomArray();
        //drawMathFun();
        drawOrderedArray();
    }

    /**
     * 画数学函数
     */
    public static void drawMathFun() {
        int N = 100;
        StdDraw.setXscale(0, N);
        StdDraw.setYscale(0, N * N);
        for (int i = 0; i < N; i++) {
            randomColor();
            StdDraw.point(i, i);
            StdDraw.point(i, i * i);
            StdDraw.point(i, i * Math.log(i));
        }
    }

    public static void drawRandomArray() {
        int N = 50;
        double[] a = new double[N];
        for (int i = 0; i < N; i++) {
            a[i] = StdRandom.uniform();
        }
        for (int i = 0; i < N; i++) {
            double x = 1.0 * i / N + 0.5 / N;
            double y = a[i] / 2.0;
            double rw = 0.5 / N;
            double rh = a[i] / 2.0;
            randomColor();
            StdDraw.filledRectangle(x, y, rw, rh);
        }
    }

    public static void drawOrderedArray() {
        int N = 50;
        double[] a = new double[N];
        for (int i = 0; i < N; i++) {
            a[i] = StdRandom.uniform();
        }
        Arrays.sort(a);
        for (int i = 0; i < N; i++) {
            double x = 1.0 * i / N + 0.5 / N;
            double y = a[i] / 2.0;
            double rw = 0.5 / N;
            double rh = a[i] / 2.0;
            randomColor();
            StdDraw.filledRectangle(x, y, rw, rh);
        }
    }

    private static void randomColor() {
        StdDraw.setPenColor(Color.getHSBColor(((float) StdRandom.uniform()), ((float) StdRandom.uniform()), (float) StdRandom.gaussian(30.0, 5)));
    }
}
